package panels;

import java.util.*;
import javax.swing.JTable;
import javax.swing.table.*;
import sqlmanager.FieldInfo;

public class ObjectBrowserModel extends AbstractTableModel {
	// fieldInfos �д�ŵ���FieldInfo����,ÿ���ֶε���ϸ��Ϣ
	public ArrayList fieldInfos = null;
	public ArrayList primaryKeys = null;
	private String[] columnNames = { "Name", "Type", "Not Null", "Auto Increament", "Primary Key" };

	public ObjectBrowserModel() {
		super();
		fieldInfos = new ArrayList();
		primaryKeys = new ArrayList();
	}

	public String getColumnName(int column) {
		return this.columnNames[column];
	}

	public int getRowCount() {
		return fieldInfos.size();
	}

	public int getColumnCount() {
		return FieldInfo.FieldsCount;
	}

	public Object getValueAt(int row, int column) {
		return ((FieldInfo) fieldInfos.get(row)).getField(column);
	}

	public void setupColumnRenderer(JTable table) {
		TableColumnModel model = table.getColumnModel();
		TableColumn column = model.getColumn(0);
		column.setCellRenderer(new FieldNameRenderer());
		column.setPreferredWidth(195);

		column = model.getColumn(1);
		column.setPreferredWidth(125);

		CheckBoxRenderer checkBoxRenderer = new CheckBoxRenderer();
		column = model.getColumn(2);
		column.setCellRenderer(checkBoxRenderer);
		column.setPreferredWidth(55);

		column = model.getColumn(3);
		column.setCellRenderer(checkBoxRenderer);
		column.setPreferredWidth(55);

		column = model.getColumn(4);
		column.setCellRenderer(checkBoxRenderer);
		column.setPreferredWidth(55);
	}

	public void clear() {
		this.fieldInfos.clear();
		this.fireTableDataChanged();
	}

	public void updatePrimaryKeyInfo() {
		int i = 0, j = 0;
		;
		String primaryKeyName = null;
		FieldInfo currentField = null;
		for (i = 0; i < this.primaryKeys.size(); i++) {
			primaryKeyName = (String) this.primaryKeys.get(i);
			for (j = 0; j < this.fieldInfos.size(); j++) {
				currentField = (FieldInfo) this.fieldInfos.get(j);
				if (primaryKeyName.equals(currentField.name)) {
					currentField.primaryKey = true;
					continue;
				}
			}
		}
	}
}
